/**
 * This class keeps track of the best times for each track in game.
 * Class is serializable to enable saving data to disk.
 * 
 * By Peter Steiner and Fredrik Wallenius.
 * DP08 - KTH
 * 2009
 */

package gameInitialization;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;


/**
 * 
 * This class keeps track of the best times for each track in game.
 * Class is serializable to enable saving data to disk.
 * 
 * @author Peter Steiner and Fredrik Wallenius.
 * @author DP08 - KTH
 *
 */
public class highScoreHandler implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -5232138518538148910L;
	private ArrayList<highScore> tracks;
	
	/**
	 * This class keeps track of the best times for each track in game.
	 * Class is serializable to enable saving data to disk.
	 *  
	 */
	public highScoreHandler(){
		tracks = new ArrayList<highScore>();
		
		for(int i = 0; i<10; i++){
			tracks.add(new highScore());
		}
	}
	
	/**
	 * Adds a new time for one track
	 * 
	 * @param track Index of the track which has been completed. Range from 0..*
	 * @param name_ The name of the player.
	 * @param time_ The time (in seconds):
	 */
	public void addNewHighScore(int track, String name_, int time_){
		tracks.get(track).addNewHighscore(name_, time_);
	}
	
	/**
	 * Gets the name of the high score holder.
	 * 
	 * @param track The index of the track.
	 * @param index The position on the high score list.
	 * @return A string with the name.
	 */
	public String getHolderName(int track, int index){
		try {
			return tracks.get(track).getHighscoreHolderName(index);
		} catch (Exception e){
			return new String();
		}
	}
	
	/**
	 * Gets the time of the high score holder.
	 * 
	 * @param track The index of the track.
	 * @param index The position on the high score list.
	 * @return The time in seconds.
	 */
	public int getHolderTime(int track, int index){
		try {
			return tracks.get(track).getHighScoreHolderTime(index);
		} catch (Exception e){
			return 0;
		}
	}
	
	
/** Private help classes below this point **/

	private class highScore implements Serializable {
		
		/**
		 * 
		 */
		private static final long serialVersionUID = -3769478371614939549L;
		private ArrayList<highScoreHolder> highScoreList;
	
	
	
		private highScore(){
			highScoreList = new ArrayList<highScoreHolder>();
		}
		
		private void addNewHighscore(String name_, int time_){
			highScoreHolder temp = new highScoreHolder();
			temp.sethName(name_);
			temp.sethTime(time_);
			highScoreList.add(temp);
			Collections.sort(highScoreList);
		}
		
		private String getHighscoreHolderName(int index){
			if(index < highScoreList.size())
				return highScoreList.get(index).gethName();
			else return new String("");
		}
		
		private int getHighScoreHolderTime(int index){
			if(index < highScoreList.size())
				return highScoreList.get(index).gethTime();
			else return 0;
		}
		
		protected class highScoreHolder implements Comparable<highScoreHolder>, Serializable {
				
				/**
			 * 
			 */
				private static final long serialVersionUID = -3288457598721021549L;
				private String hName;
				private int hTime;
				
		
				@Override
				public int compareTo(highScoreHolder o) {
					return this.hTime-o.gethTime();
				}
				
				public Boolean equals(highScoreHolder o){
					if (this.hTime == o.hTime) return true;
					return false;
				}
				
				public void sethName(String hName) {
					this.hName = hName;
				}
				public String gethName() {
					return hName;
				}
				public void sethTime(int hTime) {
					this.hTime = hTime;
				}
				public int gethTime() {
					return hTime;
				}
				
			}
	}

}